<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="活动类型" prop="promotionType">
        <el-select v-model="queryParams.promotionType" placeholder="活动类型" clearable style="width: 240px">
          <el-option v-for="dict in dict.type.app_promotion_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="活动ID：" prop="promotionId">
        <el-input v-model="queryParams.promotionId" clearable />
      </el-form-item>
      <el-form-item label="客户名称：" prop="purchaserName">
        <el-input v-model="queryParams.purchaserName" clearable />
      </el-form-item>
      <el-form-item label="客户编号：" prop="businessCode">
        <el-input v-model="queryParams.businessCode" clearable />
      </el-form-item>
      <el-form-item label="下单时间">
        <el-date-picker v-model="dateRange" style="width: 230px" value-format="yyyy-MM-dd" clearable
          type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleEmport"
          v-hasPermi="['statistics:promotion:usage:export']">导出汇总</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleDetailEmport"
          v-hasPermi="['statistics:promotion:usage:detail:export']">导出明细</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table border v-loading="loading" :data="couponUsageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户名称" prop="purchaserName" width="300" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="客户编号" prop="businessCode" width="100" align="center" />
      <el-table-column label="订单编号" prop="orderSn" align="center" width="180">
        <template slot-scope="scope">
          <router-link :to="'/order/order-detail/' + scope.row.orderId" class="link-type">
            <span>{{ scope.row.orderSn }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" prop="orderStatus" align="center" width="100" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_order_status" :value="scope.row.orderStatus" />
        </template>
      </el-table-column>
      <el-table-column label="支付状态" prop="payStatus" align="center" width="100" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_pay_status" :value="scope.row.payStatus" />
        </template>
      </el-table-column>
      <el-table-column label="支付金额" prop="flowPrice" align="center" width="100" />
    </el-table>
    <!-- 分页 -->
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import { pagePromotionUsage } from "@/api/statisticsAdmin/promotionUsage"

export default {
  name: "PromotionUsage",
  dicts: ["app_promotion_type", "order_order_status", "order_pay_status"],
  data () {
    return {
      // 遮罩层
      loading: false,
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      title: "",
      // 列表
      couponUsageList: [],
      // 总条数
      total: 0,
      // 日期范围
      dateRange: [],
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        couponId: '',
      },
    };
  },
  created () {},
  methods: {
    /** 查询活动使用列表 */
    getList () {
      if(!this.queryParams.promotionType){
        this.$modal.msgError("请选择活动类型");
        return;
      }
      this.queryParams.promotionId = this.queryParams.promotionId
      if(!this.queryParams.promotionId){
        this.$modal.msgError("请输入活动ID");
        return;
      }

      this.loading = true;
      pagePromotionUsage(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.couponUsageList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParams = { pageNum: 1, pageSize: 10, couponId: '', purchaserName: '', businessCode:'' }
      this.dateRange = []
      this.handleQuery();
    },
    handleSelectionChange(){

    },
    handleEmport(){
      if(!this.queryParams.promotionType){
        this.$modal.msgError("请选择活动类型");
        return;
      }
      this.queryParams.promotionId = this.queryParams.promotionId
      if(!this.queryParams.promotionId){
        this.$modal.msgError("请输入活动ID");
        return;
      }

      this.download(
        "statistics/promotion/usage/export",
        {
          ...this.addDateRange(this.queryParams, this.dateRange)
        },
        `活动使用汇总列表_${this.parseTime(new Date().getTime(), "{y}-{m}-{d}_{h}:{i}:{s}")}.xlsx`
      );
    },
    handleDetailEmport(){
      if(!this.queryParams.promotionType){
        this.$modal.msgError("请选择活动类型");
        return;
      }
      this.queryParams.promotionId = this.queryParams.promotionId
      if(!this.queryParams.promotionId){
        this.$modal.msgError("请输入活动ID");
        return;
      }

      this.download(
        "statistics/promotion/usage/detail/export",
        {
          ...this.addDateRange(this.queryParams, this.dateRange)
        },
        `活动使用明细列表_${this.parseTime(new Date().getTime(), "{y}-{m}-{d}_{h}:{i}:{s}")}.xlsx`
      );
    },
  }
};
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
  top: 0;
  .el-dialog__body {
    padding-top: 0;
    padding-bottom: 0;
    .DistributionCouponTitle {
      padding: 10px 10px 10px 20px;
      font-weight: 700;
      span {
        margin-left: 20px;
      }
    }
  }
}
</style>
